package v1

import (
	"github.com/gin-gonic/gin"
	"hytx_media/pkg/app"
	"hytx_media/models"
	"hytx_media/service/media_user_service"
	"hytx_media/service/setting_service"
	"net/http"
	"regexp"
	"hytx_media/service/sms_service"
	"hytx_media/pkg/e"
	"github.com/Unknwon/com"
)

func SettingProfile(c *gin.Context) {
	appG := app.Gin{c}
	userId := c.MustGet("user").(*models.MediaUser).ID

	data := media_user_service.GetUserInfo(userId)

	appG.Success(data)
}

func Setting(c *gin.Context) {
	user := c.MustGet("user").(*models.MediaUser)
	userId := user.ID
	//修改账户名
	isExpire := setting_service.UserInfoChangeExpire(userId)
	if !isExpire {
		c.JSON(http.StatusBadRequest, map[string]interface{}{
			"code": "400",
			"msg":  "账户一个月只能修改一次",
		})
		return
	}
	data := make(map[string]interface{})
	data["account"] = c.PostForm("account")
	data["introduce"] = c.PostForm("introduce")
	data["avatar"] = c.PostForm("avatar")
	data["operators_name"] = c.PostForm("operators_name")
	data["operators_id_no"] = c.PostForm("operators_id_no")
	data["email"] = c.PostForm("email")
	webSite := c.PostForm("web_site")
	err := setting_service.Update(user, data, webSite)
	if err != nil {
		c.JSON(http.StatusInternalServerError, map[string]interface{}{
			"code": 500,
			"msg":  "请稍后再试",
		})
		return
	}
	r := app.Gin{c}
	r.Success(nil)
}

func BindInfo(c *gin.Context) {
	user := c.MustGet("user").(*models.MediaUser)

	data := setting_service.GetBindInfo(user)
	c.JSON(http.StatusOK, map[string]interface{}{
		"code": 200,
		"msg":  "success",
		"data": data,
	})
}

func BindPhone(c *gin.Context) {
	phone := c.PostForm("phone")
	user := c.MustGet("user").(*models.MediaUser)
	regex := `^1[345789][0-9]{9}$`
	reg := regexp.MustCompile(regex)
	if !reg.MatchString(phone) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": e.SmsFormatError,
			"msg":  e.GetMsg(e.SmsFormatError),
		})
		return
	}
	code := c.PostForm("code")
	//验证验证码
	smsService := sms_service.SMSValidate{MobilePhone: phone, MsgCode: code}
	ck, _ := smsService.Check()

	if !ck {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": e.ERROR_SMS_VALIDATE,
			"msg":  e.GetMsg(e.ERROR_SMS_VALIDATE),
		})
		return
	}
	//修改用户手机
	err := models.DB.Table("media_users").Where("id=?", user.ID).Update("phone", phone).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": e.Error,
			"msg":  e.GetMsg(e.Error),
		})
		return
	}
	c.JSON(http.StatusCreated, gin.H{
		"code": e.SUCCESS,
		"msg":  e.GetMsg(e.SUCCESS),
	})
}

func SetWatermark(c *gin.Context) {
	user := c.MustGet("user").(*models.MediaUser)
	isWater := com.StrTo(c.PostForm("is_water")).MustInt()

	//修改
	err := models.DB.Table("media_user_settings").Where("media_user_id=?", user.ID).Update("watermark", isWater).Error
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code": e.Error,
			"msg":  e.GetMsg(e.Error),
		})
		return
	}
	c.JSON(http.StatusCreated, gin.H{
		"code": e.SUCCESS,
		"msg":  e.GetMsg(e.SUCCESS),
	})
}

func ValidateOldPhone(c *gin.Context) {
	phone := c.PostForm("phone")
	regex := `^1[345789][0-9]{9}$`
	reg := regexp.MustCompile(regex)
	if !reg.MatchString(phone) {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": e.SmsFormatError,
			"msg":  e.GetMsg(e.SmsFormatError),
		})
		return
	}
	code := c.PostForm("code")
	//验证验证码
	smsService := sms_service.SMSValidate{MobilePhone: phone, MsgCode: code}
	ck, _ := smsService.Check()

	if !ck {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": e.ERROR_SMS_VALIDATE,
			"msg":  e.GetMsg(e.ERROR_SMS_VALIDATE),
		})
		return
	}
	c.JSON(http.StatusCreated, gin.H{
		"code": e.SUCCESS,
		"msg":  e.GetMsg(e.SUCCESS),
	})
}

func Jurisdiction(c *gin.Context) {
	user := c.MustGet("user").(*models.MediaUser)
	c.JSON(http.StatusOK, gin.H{
		"code": e.SUCCESS,
		"msg": e.GetMsg(e.SUCCESS),
		"data": map[string]interface{}{
			"status": user.Status,
			"score": user.Score,
		},
	})
}
